package wwg.order.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import wwg.order.dto.OrderDTO;
import wwg.order.dto.base.ObjectResponse;
import wwg.order.entity.TOrder;
import wwg.order.enums.RspStatusEnum;
import wwg.order.mapper.TOrderMapper;

import java.util.ArrayList;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wwg
 */
@Service
@Slf4j
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements ITOrderService {


    /**
     * 创建订单
     * @Param:  OrderDTO  订单对象
     * @Return:  OrderDTO  订单对象
     */
    @Override
    public ObjectResponse<OrderDTO> createOrder(OrderDTO orderDTO) {


        ObjectResponse<OrderDTO> response = new ObjectResponse<>();

        //生成订单号
        orderDTO.setOrderNo(UUID.randomUUID().toString().replace("-",""));
        //生成订单
        TOrder tOrder = new TOrder();
        BeanUtils.copyProperties(orderDTO,tOrder);
        tOrder.setCount(orderDTO.getOrderCount());
        tOrder.setAmount(orderDTO.getOrderAmount().doubleValue());
        try {
            baseMapper.createOrder(tOrder);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            response.setStatus(RspStatusEnum.FAIL.getCode());
            response.setMessage(RspStatusEnum.FAIL.getMessage());
            return response;
        }

        response.setStatus(RspStatusEnum.SUCCESS.getCode());
        response.setMessage(RspStatusEnum.SUCCESS.getMessage());
        return response;
    }

    @Override
    public ObjectResponse<OrderDTO> createBatch(OrderDTO orderDTO) {

        ArrayList<TOrder> objects = new ArrayList<>();
        for (int i = 0; i < 5000; i++) {
            objects.add(new TOrder("orderNo"+1, "userId"+1, "commodityCode", i, 1.01));
        }
        this.saveBatch(objects);
        return null;
    }
}
